<template>
  <base-breadcrumb :title="$route.meta.title">
    <!--安监-安监人员监督日志查看 -->
    <a-form-model
      ref="ruleForm"
      :model="form"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      :rules="editModel == 0 ? {} : rules"
    >
      <a-spin :spinning="loading">
        <!-- 表格 -->
        <a-card :bordered="false">
          <title-name title="基本信息"></title-name>
          <custom-form :formList="formList" :form="form" :is-emit="false" :editModel="editModel == 0 ? '' : ''" @selectOrg="selectOrg">
            <!-- 附件上传 -->
            <template #fileInfoList>
              <template v-if="editModel != 0">
                <l-table businessCode="FACE09" :uploadArrys.sync="form.fileInfoList"></l-table>
              </template>
              <file-link v-else :fileList="form.fileInfoList" style="margin-top: -3px"></file-link>
            </template>
          </custom-form>
        </a-card>
        <!-- 页脚操作栏 -->
        <div class="btn_box page-btn-right-top" v-if="editModel != 0">
          <a-button
            class="button_right"
            type="primary"
            :loading="loadingBtn.SUBMITTED"
            @click="handleSubmit('SUBMITTED')"
            >提交</a-button
          >
          <a-button @click="handleActiveClose">关闭</a-button>
        </div>
        <div class="btn_box page-btn-right-top" v-else>
          <a-button @click="handleActiveClose">关闭</a-button>
        </div>
      </a-spin>
    </a-form-model>
     
  </base-breadcrumb>
</template>

<script>
import { addinsertSelective } from '@/api/safetySupervision/monitorLog'
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 分公司
export default {
  name: 'a' + Date.now(),
  extends: new BaseEditPage(),
  components: {
    SelectSubCompany,
    SelectProject
  },
  data() {
    return {
      loading: false, // 加载控件
      visible: false, //人员选择控件
      editModel: 0, // 操作状态:0 查看;1 编辑;2 新增
      form: { projectId:null,projectName:null,statileDate:null}, // 表单
      id: '',
      loadingBtn: {
        DELETE: false,
        DRAFT: false,
        SUBMITTED: false,
        REJECTED: false
      },
      rules: {
        statileDate: [{ required: true, message: '请选择日志时间' }],
        remark: [{ required: true, message: '请输入其它', trigger: 'blur' },],
      }, // 校验规则
      init: false, // 初始化控件
      daStatileDate:null, //默认时间
    }
  },
  async mounted() {
    // this.editModel = +this.$route.query.editModel
    this.$route.meta.title = ['安全监督日志 查看', '安全监督日志 编辑', '安全监督日志 新增'][this.editModel]

       if (this.editModel != 2) {

    } else {
      this.$nextTick(() => {
          let postCode =  this.$userInfo().currentPost.postCode,
          postsArr =  this.$userInfo().employee.posts,
          employeeId = this.$userInfo().employee.id,
          allPost = postsArr.filter((item)=>item.code===postCode);

        // 默认映射联系人为登陆人
         this.form.creatorName=  this.$userInfo().name;
         this.form.userId = this.$userInfo().id;
         this.form.employeeId = employeeId;
         this.form.branchCompanyName=  this.$userInfo().currentPost.orgName;
         this.form.creatorDept = allPost && allPost[0].name;
         this.form.projectId = this.$userInfo().currentPost.projectId || this.$userInfo().currentPost.orgId
         this.init = true
      })
    }
  },
  computed: {
    formList() {
      return [
        {
          label: '所属单位',
          type: 'orgTree',
          keyName: 'projectId',
          rules: [{ required: true, message: '请选择项目名称', trigger: 'change' }],
          span: 16,
          isAutoSelectFirst:true,
          isNeedAuth:true,
          checkStrictly: true,
          disabled:true,
          statusSet:[1,2,3,4,5,6],
          isQueryProjecDetail: false
        },
        //  {
        //   type: SelectSubCompany,
        //   keyName: 'branchCompanyId',
        //   label: '所属分公司',
        //   placeholder: '自动带出',
        //   disabled: true,
        //   attrs: {
        //     isDefaultSelected: this.editModel == 2,
        //     autoSelected: this.editModel == 2
        //   },
        //    selectChange: (e) => {
        //   }
        // },
        // {
        //   type: SelectProject,
        //   keyName: 'projectId',
        //   label: '所属项目',
        //   placeholder: '自动带出',
        //   disabled: true,
        //   attrs: {
        //     isDefaultSelected: this.editModel == 2,
        //     autoSelected: this.editModel == 2,
        //     keyValue: 'branchCompanyId',
        //     isClear: this.editModel == 2,
        //     isEdit: this.editModel != 2,
        //     isMaintenance:true,
        //     form: this.form
        //   },
        //    selectChange: (e) => {
        //     this.form.projectName = e?.name
        //   }
        // },
        {
          keyName: 'statileDate',
          type: 'a-date-picker',
          placeholder: '请选择日期',
          disabled: this.editModel == 0,
           label: '日志时间',
            attrs: {
                valueFormat: 'YYYY-MM-DD',
                disabledDate: this.disabledDate,
                defaultValue:this.formatDate(new Date()),
            }
         },
          {
          keyName: 'creatorName',
          type: 'BaseInput',
          disabled: true,
          label: '填报人'
        },
        {
          keyName: 'creatorDept',
          type: 'BaseInput',
          disabled: true,
          label: '填报人职务'
        },
        {
          keyName: 'remark',
          type: 'BaseTextarea',
          label: '其它',
          disabled: this.editModel == 0,
          span: 24,
          autoSize:{ minRows: 30, maxRows: 30 },
          attrs: {
              maxLength : 3000,
            },
        },
      ]
    }
  },
  methods: {
    selectOrg(val) {
      this.form.branchCompanyId = val[val.length - 2].orgId;
      this.form.projectName = val[val.length - 1].projectName;
      this.form.projectId = val[val.length - 1].projectId
    },
    disabledDate(current) {
        return current && (current < this.moment().subtract(2, 'day') || current > this.moment().subtract(0, 'day'))
    },
       //格式化当前日期
    formatDate(date, type = '-') {
      let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      let daStatileDate = date.getFullYear() + type + M + type + D;
      this.daStatileDate = daStatileDate;
      return daStatileDate
    },
    validateForm() {
      let flag = false
      this.$refs.ruleForm.validate((e) => {
        if (e) {
          flag = true
        }else {
          this.$message.warning('请填写完所有必填项信息')
        }
      })
      return flag
    },
    /**
     * 提交表单
     */
   async handleSubmit(status) {
      if(!this.form.statileDate){
         this.$set(this.form,'statileDate',this.daStatileDate);
      }
      if (status == 'SUBMITTED' && !this.validateForm()) {
        return
      }
      this.loading = true
      this.loadingBtn[status] = true
      let form = this.$clone(this.form);
      form.queryType = this.queryType;
      let api = addinsertSelective;
      api(form)
        .then(() => {
          this.loading = false
          this.loadingBtn[status] = false
          this.$message.success(status == 'SUBMITTED' ? '提交成功！' : '保存成功')
          this.$multiTab.close()
        })
        .catch(() => {
          this.loadingBtn[status] = false
          this.loading = false
          this.$message.warning(err.data.message)
        })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
